


//
//  CustomeTabBar.m
//  KamunShangCheng
//
//  Created by Michael Li on 2018/7/20.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "CustomeTabBar.h"
#import "MyCollectionIconModel.h"
@interface CustomeTabBar()
@property(nonatomic,strong) MCButton *tabBtn;
@property(nonatomic,strong) MCButton *tabPreBtn;
@property(nonatomic,strong) UIView *customeView;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSMutableArray *iconMuta;
@property(nonatomic,strong) NSString *urlString;

@end
@implementation CustomeTabBar
-(NSMutableArray *)iconMuta
{
    if (!_iconMuta) {
        _iconMuta = [NSMutableArray array];
        
    }
    return _iconMuta;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundImage:[ImageColorHelper createImageWithColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1]]];
        self.manager = [AFHTTPSessionManager manager];
        UIImageView *bgV = [[UIImageView alloc] initWithFrame:self.bounds];
        bgV.image =[ImageColorHelper createImageWithColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1]];
        self.backgroundImage = bgV.image;
        self.shadowImage = [UIImage new];
        self.customeView = [[UIView alloc]initWithFrame:self.bounds];
        [self addSubview:self.customeView];
        CGFloat w = self.frame.size.width/5;
        CGFloat x = 0;
        CGFloat h = self.frame.size.height;
        CGFloat y = 0;
        XLog(@"h:%f",h);
        for (int i = 0; i < 5; i++) {
            x = w*i;
//            if (i >= 2) {
//                x = w*(i+1);
//            }
            self.tabBtn = [[MCButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
            [self.customeView addSubview:self.tabBtn];
            self.tabBtn.tag = 200+i;
            [self.tabBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            self.tabBtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [self.tabBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
            self.urlString = [NSString stringWithFormat:@"%@/icon/selectFiveth",kUrl];
            
            NSArray *titles = @[@"首页",@"需求大厅",@"发布",@"服务大厅",@"我的"];
            [self.tabBtn setTitle:titles[i] forState:UIControlStateNormal];
            [self.tabBtn setTitleColor:kOrangeBg forState:UIControlStateSelected];
            [self.tabBtn setBackgroundColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1]];
            NSArray *imageNormalA = @[@"replace_icon_71",@"replace_icon_73",@"replace_icon_59",@"replace_icon_75",@"replace_icon_77"];
            NSArray *imageSelectedA = @[@"replace_icon_62",@"replace_icon_65",@"replace_icon_59",@"replace_icon_67",@"replace_icon_69"];

            [self.tabBtn setImage:[UIImage imageNamed:imageNormalA[i]] forState:UIControlStateNormal];
            [self.tabBtn setImage:[UIImage imageNamed:imageSelectedA[i]] forState:UIControlStateSelected];
                self.tabBtn.buttonStyle = imageCustomeTop;
            if (i == 2) {
                self.tabBtn.buttonStyle = imageTabBarTop;
                [self.tabBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateSelected];
                
            }
                XLog(@"self.tabBtnImage1:%@",self.tabBtn.imageView.image);
            if (i == 0) {
                self.tabBtn.selected = YES;
                self.tabPreBtn = self.tabBtn;
                
            }
            
        }
        [self.manager POST:self.urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSArray *date = [responseObject objectForKey:@"date"];
            for (NSDictionary *dict in date) {
                MyCollectionIconModel *model = [MyCollectionIconModel mj_objectWithKeyValues:dict];
                [self.iconMuta addObject:model];
            }
            XLog(@"self.iconMuta:%@",self.iconMuta);
            for (int i = 0; i < 5; i++) {
                if (i == 0) {
                    MyCollectionIconModel *model = self.iconMuta[0];
                    MyCollectionIconModel *modelSeleect = self.iconMuta[5];
                    UIButton *btn = (UIButton *)[self viewWithTag:200];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"replace_icon_71"]];
                    [btn sd_setImageWithURL:[NSURL URLWithString:modelSeleect.icon] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"replace_icon_62"]];
                    
                }
                if (i == 1) {
                    MyCollectionIconModel *model = self.iconMuta[1];
                    MyCollectionIconModel *modelSeleect = self.iconMuta[6];
                    UIButton *btn = (UIButton *)[self viewWithTag:201];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"replace_icon_73"]];
                    [btn sd_setImageWithURL:[NSURL URLWithString:modelSeleect.icon] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"replace_icon_65"]];
                    
                }
                if (i == 2) {
                    MyCollectionIconModel *model = self.iconMuta[4];
//                    MyCollectionIconModel *modelSeleect = self.iconMuta[2];
                    UIButton *btn = (UIButton *)[self viewWithTag:202];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"replace_icon_59"]];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"replace_icon_59"]];
                    
                }
                if (i == 3) {
                    MyCollectionIconModel *model = self.iconMuta[2];
                    MyCollectionIconModel *modelSeleect = self.iconMuta[7];
                    UIButton *btn = (UIButton *)[self viewWithTag:203];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"replace_icon_75"]];
                    [btn sd_setImageWithURL:[NSURL URLWithString:modelSeleect.icon] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"replace_icon_67"]];
                    
                }
                if (i == 4) {
                    MyCollectionIconModel *model = self.iconMuta[3];
                    MyCollectionIconModel *modelSeleect = self.iconMuta[8];
                    UIButton *btn = (UIButton *)[self viewWithTag:204];
                    [btn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"replace_icon_77"]];
                    [btn sd_setImageWithURL:[NSURL URLWithString:modelSeleect.icon] forState:UIControlStateSelected placeholderImage:[UIImage imageNamed:@"replace_icon_69"]];
                    
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    return self;
}
-(void)clickBtn:(MCButton *)btn
{
    btn.selected = !btn.selected;
    if ([self.delegate respondsToSelector:@selector(customeTabBar:withInterget:)]) {
        [self.delegate customeTabBar:self withInterget:btn.tag];
    }
    if (btn.tag != 202) {
        if (self.tabPreBtn != btn) {
            self.tabPreBtn.selected = NO;
        }else {
            btn.selected = YES;
        }
        self.tabPreBtn = btn;
        
    }
}

-(void)clickCustomeBtn:(MCButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(customeTabBar:withCustomeBtn:)]) {
        [self.delegate customeTabBar:self withCustomeBtn:btn];
    }

}
@end
